package com.zx.cloud.controller;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.Shift;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.common.mapper.JsonMapper;
import com.zx.cloud.common.token.PasswordHashUtil;
import com.zx.cloud.domain.ClientInfo;
import com.zx.cloud.domain.User;
import com.zx.cloud.domain.UserRole;
import com.zx.cloud.model.request.ManagerLoginRequest;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.model.response.Response;
import com.zx.cloud.service.LogService;
import com.zx.cloud.service.ResourceService;
import com.zx.cloud.service.UserRoleService;
import com.zx.cloud.service.UserService;
import com.zx.cloud.util.Page;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户表
 */
@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX+"/user")
public class UserController{

	private static final Logger logger=Logger.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private LogService  logService; 
	
	@ApiOperation(value = "添加用户", notes = "添加用户")
	@RequestMapping(value = "/addDemo", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> addDemo(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@Valid @RequestBody  ManagerLoginRequest user, BindingResult error){
		return new ObjectDataResponse<Integer>();
	}
	@ApiOperation(value = "用户管理-用户列表", notes = "用户管理-用户列表")
	@RequestMapping(value = "/getUserList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<User>> getUserList(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody User user){
		user.setOrderBy("SYS_USER.id DESC");//降序
		Page<User> datas=userService.page(user);
		return new ObjectDataResponse<Page<User>>(datas);
	}
	@ApiOperation(value = "用户管理-新增用户", notes = "用户管理-新增用户")
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> addUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) throws NoSuchAlgorithmException, InvalidKeySpecException{
		String username=user.getUsername();
		String phone=user.getPhone();
		if(StringUtils.isEmpty(username)){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[username]用户名称为空啦");
		}
		if(StringUtils.isEmpty(phone)){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[phone]手机号码为空啦");
		}else if(!phone.matches("^[1]{1}[3,5,7,8]{1}(\\d){9}$")){
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[phone]手机号码格式错误啦");
		}
		user.setCreateDate(OffsetDateTime.now());//创建时间
		int success=userService.insert(user);
		logService.addLog(user.getId(), "新增用户", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	@ApiOperation(value = "用户管理-修改用户", notes = "用户管理-修改用户")
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> updateUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		String username=user.getUsername();
		String phone=user.getPhone();
		if(StringUtils.isEmpty(username)){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[username]用户名称为空啦");
		}
		if(StringUtils.isEmpty(phone)){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[phone]手机号码为空啦");
		}else if(!phone.matches("^[1]{1}[3,5,7,8]{1}(\\d){9}$")){
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[phone]手机号码格式错误啦");
		}
		user.setUpdateDate(OffsetDateTime.now());//修改时间
		int success=userService.updateUser(user);
		logService.addLog(user.getId(), "修改用户", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "用户管理-删除用户", notes = "用户管理-删除用户")
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> deleteUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		try {
			user.getId().toString();
		} catch (Exception e) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]用户主键为空啦");
		}
		user.setUpdateDate(OffsetDateTime.now());//修改时间
		int success=userService.deleteUser(user);
		logService.addLog(user.getId(), "删除用户", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "用户管理-禁用用户", notes = "用户管理-禁用用户")
	@RequestMapping(value = "/banUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> banUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		try {
			user.getId().toString();
		} catch (Exception e) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]用户主键为空啦");
		}
		user.setUpdateDate(OffsetDateTime.now());//修改时间
		int success=userService.banUser(user);
		logService.addLog(user.getId(), "禁用用户", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "用户管理-启用用户", notes = "用户管理-启用用户")
	@RequestMapping(value = "/startUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> startUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		try {
			user.getId().toString();
		} catch (Exception e) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]用户主键为空啦");
		}
		user.setUpdateDate(OffsetDateTime.now());//修改时间
		int success=userService.startUser(user);
		logService.addLog(user.getId(), "启用用户", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "用户管理-查看用户", notes = "用户管理-查看用户")
	@RequestMapping(value = "/getUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<User> getUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) throws NoSuchAlgorithmException, InvalidKeySpecException{
		try {
			user.getId().toString();
		} catch (Exception e) {
			Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]用户主键为空啦");
		}
		User success=userService.getUser(user);
		
		return new ObjectDataResponse<User>(success);
	}
	
	@ApiOperation(value = "用户管理-根据账号查看用户", notes = "用户管理-根据账号查看用户")
	@RequestMapping(value = "/accountQuery", method = RequestMethod.POST)
	@ResponseBody
	public Response accountQuery(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) throws NoSuchAlgorithmException, InvalidKeySpecException{
		user.setOrderBy("SYS_USER.id DESC");//降序
		Page<User> datas=userService.accountQuery(user);
		logService.addLog(user.getId(), "搜索用户", 0);
		return new ObjectDataResponse <Page<User>>(datas);
	}
	@ApiOperation(value = "用户管理-首页统计数据信息", notes = "用户管理-首页统计数据信息")
	@RequestMapping(value = "/statisticalInformation", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse <Map<String, Object>> statisticalInformation(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId){
		
		Map<String, Object> map = userService.getHomeInfoToOrder();
		
		return new ObjectDataResponse <Map<String, Object>>(map);
	}
	
	@ApiOperation(value = "用户管理-重置密码", notes = "用户管理-重置密码")
	@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> resetPassword(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		user.setUpdateDate(OffsetDateTime.now());//修改时间
		int success=userService.resetPassword(user);
		logService.addLog(user.getId(), "重置用户密码", 0);
		return new ObjectDataResponse<Integer>(success);
	}
}
